<script setup>
import { onMounted, ref } from 'vue'
import { artGetArticleListService, artDelArticleService } from '@/api/article'
import { formatTime } from '@/utils/format'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'

//加载效果
const isLoading = ref(false)

//获取文章列表
const getArticleList = async () => {
  const { data, total } = await artGetArticleListService(params.value)
  articleList.value = data
  totalArticle.value = total
}
//请求文章列表数据
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
//存放列表数据
const articleList = ref([])
//总数
const totalArticle = ref(0)
onMounted(async () => {
  isLoading.value = true
  await getArticleList()
  isLoading.value = false
})
//分页
//单页数据大小变化，要回到第一页，当前页位置已经换了没意义
const onSizeChange = async () => {
  isLoading.value = true
  //重新请求数据
  params.value.pagenum = 1
  await getArticleList()
  isLoading.value = false
}
//切换页码
const onCurrentChange = async () => {
  isLoading.value = true
  await getArticleList()
  isLoading.value = false
}

//获取抽屉
const drawerRef = ref(null)
//增加文章
const onAddArticle = () => {
  drawerRef.value.open({})
}
const onEditArticle = (row) => {
  drawerRef.value.open(row)
}
//删除文章
const onDeleteArticle = async (row) => {
  //确认删除
  await ElMessageBox.confirm('确定删除吗?', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelArticleService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}
//更新回显
const onSuccess = async (state) => {
  //如果是添加，总数要加一,跳到最后一页
  if (state === 'add') {
    params.value.pagenum = Math.ceil(
      (totalArticle.value + 1) / params.value.pagesize
    )
  }
  getArticleList()
}

//搜索功能,因为是响应式，选定后自动修改params，根据最新params去请求数据
const onSearch = async () => {
  isLoading.value = true
  params.value.pagenum = 1 //可能搜索出来的数据页数不足到原来的页数，所以先回到第一页
  await getArticleList()
  isLoading.value = false
}
const reSet = async () => {
  params.value = {
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
  }
  await getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类：">
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="reSet">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" v-loading="isLoading">
      <el-table-column prop="title" label="标题" width="180" />
      <el-table-column prop="cate_name" label="分类" width="180" />
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            plain
            :icon="Edit"
            circle
            @click="onEditArticle(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            plain
            :icon="Delete"
            circle
            @click="onDeleteArticle(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 5, 7]"
        background
        layout="jumper,total, sizes, prev, pager, next"
        :total="totalArticle"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </div>
  </page-container>
  <article-edit ref="drawerRef" @success="onSuccess"></article-edit>
</template>
<style lang="scss" scoped>
.el-select {
  --el-select-width: 200px;
}
:deep {
  .el-select {
    --el-select-width: 200px;
  }
}
</style>
